package com.seed.uaa.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.StrUtil;
import com.seed.application.A;
import com.seed.application.Sa;
import com.seed.application.env.MicroEnv;
import com.seed.core.action.Action;
import com.seed.core.exception.UserAuthenticateException;
import com.seed.core.exception.UserNotLoginException;
import com.seed.core.pojo.Subject;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.Micro;
import com.seed.util.SeedKit;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

public class CheckLoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        if(handler instanceof HandlerMethod handlerMethod) {
//            Method method = handlerMethod.getMethod();
//            Class<?> controllerClass = method.getDeclaringClass();
//            if(CrudController.class == controllerClass) {
//                String seedName = (String) request.getAttribute("seedName");
//                String actionName = (String) request.getAttribute("actionName");
//                checkLogin(seedName, actionName);
//                checkPermission(seedName, actionName);
//            } else {
//                checkLogin(controllerClass, method);
//                checkPermission(controllerClass, method);
//            }
//        }

        return true;
    }

    private void checkLogin(Class<?> controllerClass, Method method) {
        SaIgnore saIgnore = method.getAnnotation(SaIgnore.class);
        if(saIgnore == null) saIgnore = controllerClass.getAnnotation(SaIgnore.class);
        if(saIgnore == null) {
            Subject subject = A.getSubject();
            if(subject == null) throw new UserNotLoginException("请登录");
        }
    }

    private void checkLogin(String seedName, RequestMethod method, String actionName) {
        Micro micro = Sa.getMicro();
        boolean checkLogin = getCheckLogin(micro.getSeed(seedName), method, actionName);
        if(checkLogin) {
            Subject subject = A.getSubject();
            if(subject == null) throw new UserNotLoginException("请登录");
        }
    }

    private boolean getCheckLogin(Seed seed, RequestMethod method, String actionName) {
        Action action = SeedKit.getAction(seed, method, actionName);
        Boolean checkLogin = action.getCheckLogin();
        if(checkLogin != null) return checkLogin;

        checkLogin = seed.getCheckLogin();
        if(checkLogin != null) return checkLogin;

        return false;
    }

    private void checkPermission(Class<?> controllerClass, Method method) {
        SaCheckPermission saCheckPermission = method.getAnnotation(SaCheckPermission.class);
        if(saCheckPermission == null) saCheckPermission = controllerClass.getAnnotation(SaCheckPermission.class);
        if(saCheckPermission == null) return;

        Subject current = A.getCurrent();
        if(current == null) throw new UserAuthenticateException("没有权限");

        String[] permissions = saCheckPermission.value();
        String permission = permissions.length == 0 ? null : permissions[0];
        if(StrUtil.isBlank(permission)) {
            String microName = MicroEnv.getMicroName();
            String controllerName = controllerClass.getSimpleName();
            String seedName = StrUtil.lowerFirst(controllerName.substring(0, controllerName.length() - "Controller".length()));
            String actionName = method.getName();
            permission = (StrUtil.isBlank(microName) ? "" : microName + '.') + seedName + '.' + actionName;
        }

        List<String> currentPermissions = current.getPermissions();
        List<String> currentFullPermissions = current.getFullPermissions();
        boolean b1 = currentPermissions.contains("*") || currentPermissions.contains(permission);
        boolean b2 = currentFullPermissions.contains("*") || currentFullPermissions.contains(permission);
        if(b1 && b2) return;

        throw new UserAuthenticateException("没有权限");
    }

    private void checkPermission(String seedName, String actionName) {

    }
}
